import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores'
const router = createRouter({
  // 历史模式不带#
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login', name: 'login', meta: { title: '登录' }, component: () => import('@/views/login/LoginPage.vue')
    },
    {
      path: '/', name: 'root', meta: { title: '大事件' },component: () => import('@/views/layout/LayoutContainer.vue'),
      redirect: '/article/manage',
      children: [
        { path: '/article/manage',meta: { title: '文章管理' }, component: () => import('@/views/article/ArticleManage.vue') },
        { path: '/article/channel',meta: { title: '文章分类' }, component: () => import('@/views/article/ArticleChannel.vue') },
        { path: '/user/profile',meta: { title: '用户信息' }, component: () => import('@/views/user/UserProfile.vue') },
        { path: '/user/avatar',meta: { title: '用户头像' }, component: () => import('@/views/user/UserAvatar.vue') },
        { path: '/user/password',meta: { title: '修改密码' }, component: () => import('@/views/user/UserPassword.vue') }
      ]
    }
  ]
})

// 登录访问拦截 这里是拦截除了登录注册页的所有页面跳转
router.beforeEach((to) => {
  if (to.meta.title) {
    document.title = to.meta.title
  }
  const userStore = useUserStore()
  if (!userStore.token && to.path !== '/login') return '/login'
})

export default router
